shop.product = {
	conf:{
		product: {},
		itemHot: 2,
		now: TIME_NOW,
		show_sec: false
	},
	init:function(id, start, end, buyer, buyer_need, out_time, width, unit, timeType, is_end){
		width = width ? width : 140;
		shop.product.conf.product[id] = {
			id:	id,
			start: start,
			end: end,
			now: shop.product.conf.now,
			count: end - shop.product.conf.now,
			buyer: buyer,
			out_time: out_time,
			buyer_active: buyer_need,
			buyer_need: buyer_need,
			pick_time: '',
			step: width/100,
			max_width:width,
			percent: 'buyerPercent'+id, //buyerPercent
			time: 'timeCount'+id, 		//timeCount
			timeTitle: 'timeTitle'+id,	//timeTitle
			displayTime: timeType,
			counter_id : 0,
			updateTime: 2*60*1000,
			//updateTime: 15000,
			first:false,
			unit: unit,
			is_end: is_end == 1
		};

		//draw bar
		shop.product.drawBuyer(id);
		//start clock count time
		shop.product.countTime(id);
		//start updating
		if((shop.product.conf.product[id].buyer < shop.product.conf.product[id].buyer_need) &&
		   (shop.product.conf.product[id].end > shop.product.conf.now) &&
		   !shop.product.conf.product[id].is_end)
		{
			shop.product.update(id);
		}
	},
	countTime:function(id){
		shop.product.conf.product[id].now++;
		shop.product.conf.product[id].count--;
		if(shop.product.displayTime(id)){
			shop.product.conf.product[id].counter_id = setTimeout(function(){shop.product.countTime(id)},1000);
		}else{
			var title = '',timeStr='';
			if(shop.product.conf.product[id].buyer >= shop.product.conf.product[id].buyer_need){//so luong nguoi mua da du
				title = 'Thời gian hết hàng';
				timeStr = shop.product.conf.product[id].out_time;
			}else{
				var title = 'Thời gian kết thúc', time = 0;
				if(shop.product.conf.product[id].start > shop.product.conf.product[id].now){
					title = 'Thời gian bắt đầu';
					time  = shop.product.conf.product[id].start;
				}else if(shop.product.conf.product[id].end <= shop.product.conf.product[id].now || shop.product.conf.product[id].is_end){
					time  = shop.product.conf.product[id].end;
				}
				var d = new Date(time*1000),
				hour= d.getHours(),
				min = d.getMinutes(),
				day = d.getDate(),
				month = d.getMonth()+1;
				timeStr = (hour>9?'':'0')+(hour>0?hour:0)+'h'+(min>9?'':'0')+(min>0?min:0)+' ngày '+(day>9?'':'0')+(day>0?day:0)+'/'+(month>9?'':'0')+(month>0?month:0)+'/'+d.getFullYear();
			}
			//update time title
			jQuery('#'+shop.product.conf.product[id].timeTitle).html(title);
			jQuery('#'+shop.product.conf.product[id].time).html('<span class="timeOver">'+timeStr+'</span>');
		}
	},
	displayTime:function(id){
		var day = Math.floor(shop.product.conf.product[id].count/86400),
		count= shop.product.conf.product[id].count%86400,
		hour = Math.floor(count/(60*60)),
		min = Math.floor((count%(60*60))/60),
		sec = Math.floor(count - hour*60*60 - min*60),
		color = '',
		colorDeal = '#54682D',
		numberProduct = shop.product.conf.product[id].buyer_need-shop.product.conf.product[id].buyer;

		//set lai mau cho deal
		if(shop.product.conf.product[id].start < shop.product.conf.product[id].now && day <= 0 && hour <= 0 && min <= 10){
			color = 'color:red';
			colorDeal = 'red';
		}else if(numberProduct <= 10 && numberProduct > 0){
			colorDeal = 'red';
		}
		jQuery('#deal'+id).css('color',colorDeal);

		//neu het gio tra ve false
		if((shop.product.conf.product[id].buyer >= shop.product.conf.product[id].buyer_need) || (shop.product.conf.product[id].start > shop.product.conf.product[id].now) || (shop.product.conf.product[id].end < shop.product.conf.product[id].now) || shop.product.conf.product[id].is_end){
			var html = '',parent = jQuery('#productItem'+id);
			if(shop.product.conf.product[id].start < shop.product.conf.product[id].now){
				if(shop.product.conf.product[id].buyer >= shop.product.conf.product[id].buyer_need){
					html = '<div class="overTime mTop5">Đã hết cơ hội mua rẻ</div>';
				}else{
					html = '<div class="overTime mTop5">Đã hết cơ hội mua rẻ</div>';
				}
				html += '<center class="numBuyerOver">(Có <b>'+shop.product.conf.product[id].buyer+'</b> người đã mua được)</center>'
				jQuery('#event_end'+id).html(html);
				jQuery('.buying', parent).hide();
				jQuery('.stillRun', parent).hide();
				shop.product.conf.product[id].is_end = 1;
				jQuery('.buyNow', parent).addClass('buyNowOut');
			}else{
				jQuery('.buyNow', parent).addClass('buyNowWait');
			}
			return false;
		}

		if(shop.product.conf.product[id].displayTime){
			jQuery('#'+shop.product.conf.product[id].time+' .hours').html('<span style="'+color+'">'+(hour>9?'':'0')+(hour>0?hour:0)+'</span>');
			jQuery('#'+shop.product.conf.product[id].time+' .min').html('<span style="'+color+'">'+(min>9?'':'0')+(min>0?min:0)+'</span>');
			jQuery('#'+shop.product.conf.product[id].time+' .sec').html('<span style="'+color+'">'+((sec>9&&sec<60)?'':'0')+((sec>0&&sec<60)?sec:0)+'</span>');
		}else{
			var obj = shop.get_ele(shop.product.conf.product[id].time);
			if(obj){
				obj.innerHTML = '<span style="'+color+'">'+(day>0?(day+' ngày, '):'')+(hour>9?'':'0')+(hour>0?hour:0)+'<span style="text-transform:lowercase"> giờ </span> '+(min>9?'':'0')+(min>0?min:0)+' phút '+( (day<=0 || shop.product.conf.show_sec) ? (((sec>9&&sec<60)?'':'0')+((sec>0&&sec<60)?sec:0)+' giây'):"" )+'</span>';
			}else{
				return false;
			}
		}
		return hour>=0 && min >=0 && sec >= 0 && sec <= 59;
	},
	drawBuyer:function(id){
		var percent = (shop.product.conf.product[id].buyer_need == 0) ? 100 : Math.round(shop.product.conf.product[id].buyer / shop.product.conf.product[id].buyer_need * 100),
		width = Math.round(percent*shop.product.conf.product[id].step),
		divPer= shop.get_ele(shop.product.conf.product[id].percent);
		if(divPer){
			if(width > 0 && !jQuery(divPer).hasClass('buyerPercentOverZero')){
				divPer.className +=  ' buyerPercentOverZero';
			}
			if(width <= 4){
				if(!jQuery(divPer).hasClass('buyerPercentZero')){
					divPer.className += ' buyerPercentZero';
				}
				width = 4;
			}else{
				jQuery(divPer).removeClass('buyerPercentZero');
			}
			if(shop.product.conf.product[id].max_width - width <= 0){
				if(!jQuery(divPer).hasClass('buyerPercentFull')){
					divPer.className += ' buyerPercentFull';
				}
			}
			divPer.style.width = width+"px";
		}
	},
	update:function(id){
		if(!shop.product.conf.product[id].is_end){
			if(shop.product.conf.product[id].first){
				shop.ajax_popup("act=item&code=get-hot",'GET',{id:id},
				function(j){
					if(j.err == 0){
						if(j.data.id == id){
							shop.product.conf.product[id].now   =   parseInt(j.now);
							shop.product.conf.product[id].id 	=	parseInt(j.data.id);
							shop.product.conf.product[id].start =	parseInt(j.data.startTime);
							shop.product.conf.product[id].end	=	parseInt(j.data.endTime);
							shop.product.conf.product[id].count =	shop.product.conf.product[id].end - shop.product.conf.product[id].now;
							shop.product.conf.product[id].buyer =	parseInt(j.data.buyer);
							shop.product.conf.product[id].out_time = j.data.event_end;
							shop.product.conf.product[id].buyer_need = parseInt(j.data.buyer_need);
							shop.product.conf.product[id].buyer_active = parseInt(j.data.buyer_active);
							shop.product.conf.product[id].pick_time = j.data.pick_time;
							shop.product.conf.product[id].is_end = j.data.is_end == 1;
	
							var html = '', parent = jQuery('#productItem'+id);
							//update danh sach khach hang
							if(j.data.customer.length <= 0){
								html = shop.join
								('<center class="p10">')
									('<a href="javascript:void(0)" onclick="shop.cart.addItem('+j.data.id+','+j.data.startTime+','+j.data.endTime+','+j.data.buyer+','+j.data.buyer+')" title="Mua ngay">')
										('<img src="style/images/icon/icon_hand.png" alt="number one" />')
									('</a>')
									('<div class="mTop10">Hãy là <b>người đầu tiên</b> mua sản phẩm này</div>')
								('</center>')();
							}else{
								html = '<ul class="firstList">';
								var count = 1,
								total = parseInt(shop.product.conf.product[id].buyer),
								idx = 0;
								if(shop.product.conf.product[id].id == 73){
									total = 0;
									for(var t in j.data.customer){
										total++;
									}
								}
								for(var i in j.data.customer){
									idx = total - count + 1;
	
									html += shop.join
									('<li id="customer'+id+'_'+count+'"'+(count>=6?' class="hidden"':'')+'>')
									  ('<span class="f11" style="color:#999">'+shop.product.zero(idx, total)+'. </span>')
									  ('<span><a name="customer'+id+'_'+count+'">'+j.data.customer[i].name+'</a></span>')
									('</li>')();
									count++;
								}
								html +=	'</ul>';
								if(shop.product.conf.product[id].buyer >= 6){
									html += shop.join
									('<div class="more" align="right">')
										('<a href="javascript:void(0)" onclick="shop.closeMoreBuyer(this)" class="top fl" style="display:none" title="Thu lại"></a>')
										('<a href="javascript:void(0)" onclick="shop.showMoreBuyer(this)" title="Xem tiếp 20 người">Xem thêm >></a>')
										('<div class="c"></div>')
									('</div>')();
								}
							}
							jQuery('div.listBuyer>div.mainBlueBoxNew', parent).html(html);
	
							//update so nguoi mua
							if(shop.product.conf.product[id].buyer > 0){
								jQuery('.buyerStatus', parent).html('<div class="numBuyer">(Đã có <b>'+shop.product.conf.product[id].buyer+'</b> <img src="style/images/icon/onePerson.jpg" width="15" height="13" alt="người" /> mua)</div>');
							}else{
								jQuery('.buyerStatus', parent).html('');
							}
							//update sản phẩm còn
							var con = shop.product.conf.product[id].buyer_need-shop.product.conf.product[id].buyer;
							if(con > 0){
								jQuery('#deal'+id).html('<span>Còn <b>'+con+'</b> '+shop.product.conf.product[id].unit+'</span>');
							}
							jQuery('#pNumber'+id).html(shop.product.conf.product[id].buyer_need);

	
							//update tinh trang san pham
							html = '';
							if(shop.product.conf.product[id].buyer <= 0){
								html = '<div class="cBarText2">Khi có đủ <b style="color:#E11545">'+shop.product.conf.product[id].buyer_active+'</b> người mua trở lên, các đơn hàng sẽ có hiệu lực</div>';
							}else{
								if(shop.product.conf.product[id].end < shop.product.conf.product[id].now){
									jQuery('.buying').hide();
									html += '<div class="cBarText2 buying">Muachung đã hết hạn, bạn đã mất cơ hội mua sản phẩm này</div>';
								}else{
									if(shop.product.conf.product[id].buyer >= shop.product.conf.product[id].buyer_active){
										html += '<div class="cBarText2 buying"></div>';//Số lượng có hạn !
									}else{
										html += '<div class="cBarText2">(Cần thêm <b>'+(shop.product.conf.product[id].buyer_active-shop.product.conf.product[id].buyer)+'</b> người nữa để mua được giá này)</div>';
									}
								}
							}
							jQuery('.productStatus', parent).html(html).addClass('updated');
	
							//update bo dem thoi gian
							clearTimeout(shop.product.conf.product[id].counter_id);
							shop.product.countTime(id);
							shop.product.drawBuyer(id);
	
							//update lai nut mua hang
							if(
							   shop.product.conf.product[id].end < shop.product.conf.product[id].now ||
							   shop.product.conf.product[id].buyer >= shop.product.conf.product[id].buyer_need)
							{
								jQuery('.buyNow', parent).addClass('buyNowOut');
							}else if(shop.product.conf.product[id].start > shop.product.conf.product[id].now){
								jQuery('.buyNow', parent).addClass('buyNowWait');
							}else{
								jQuery('.buyNow', parent).removeClass('buyNowOut').removeClass('buyNowWait');
								//update so luong co hoi con lai
								jQuery('#buyerCount'+id).html(shop.product.conf.product[id].buyer_need - shop.product.conf.product[id].buyer);
							}
							//vong lap
							setTimeout(function(){shop.product.update(id)}, shop.product.conf.product[id].updateTime);
							return;
						}
					}
					window.location.reload();
				});
			}else{
				//lan dau tien khong load ajax
				shop.product.conf.product[id].first = true;
				setTimeout(function(){shop.product.update(id)}, shop.product.conf.product[id].updateTime);
			}
		}
	},
	zero:function(number, total){
		number = ''+number;
		total = ''+total;
		var out = number;
		number = number.length;
		total = total.length;
		for(var i=0;i<total-number;i++){
			out = '0'+out;
		}
		return out;
	},
	viewAds:function(obj,id){
		if(jQuery(obj).hasClass('views')){
			jQuery(obj).removeClass();
			jQuery('#viewAds'+id).hide();
		}
		else{
			jQuery(obj).addClass('views');
			jQuery('#viewAds'+id).show();
		}
	}
};

shop.showMoreBuyer = function(obj){
	var p = jQuery(obj).parent().parent(), count = 0, total = 20, anchor = '';
	jQuery('ul.firstList li', p).each(function(){
		if(jQuery(this).hasClass('hidden') && count < total){
			if(anchor == ''){anchor = this.id}
			jQuery(this).removeClass('hidden');
			count++;
		}
	});
	if(anchor != ''){
		jQuery('.top', p).show();
		shop.auto_scroll('#'+anchor);
	}
};

shop.closeMoreBuyer = function(obj){
	var p = jQuery(obj).parent().parent(), count = 0, limit = 5, anchor = '';
	jQuery('ul.firstList li', p).each(function(){
		count++;
		if(anchor == ''){anchor = this.id}
		if(count > limit && !jQuery(this).hasClass('hidden')){
			jQuery(this).addClass('hidden');
		}
	});
	if(anchor != ''){
		jQuery('.top', p).hide();
		shop.auto_scroll('#'+anchor);
	}
};

shop.auto_scroll = function(anchor) {
    var target = jQuery(anchor);
    target = target.length && target || jQuery('[name=' + anchor.slice(1) + ']');
    if (target.length) {
        var targetOffset = target.offset().top;
        jQuery('html,body').animate({scrollTop: targetOffset},1000);
        return false;
    }
	return true;
};

shop.activeTabDeal = function(weekly_deal){
	if(weekly_deal){
		jQuery('.buttonDeal').removeClass('buttonDealactive');
		jQuery('.buttonDealWeek').addClass('buttonDealWeekactive');
	}else{
		jQuery('.buttonDeal').addClass('buttonDealactive');
		jQuery('.buttonDealWeek').removeClass('buttonDealWeekactive');
	}
};